package com.demo.chartview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;

import com.demo.chartview.databinding.ActivityMainBinding;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.utils.Fill;
import com.setting.defbarchartutils.DefBarChartSetting;
import com.setting.deflinechartutils.DefLineChartSetting;
import com.setting.deflinechartutils.PointDataView;
import com.setting.defpiechartutils.DefPieChartSetting;
import com.setting.model.Datapiechart;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding mDataBinding;
    //曲线图数据
    private List<String> lines = new ArrayList<>();
    private List<Entry> values = new ArrayList<>();
    private List<Entry> values2 = new ArrayList<>();
    //饼状图数据
    private List<PieEntry> listdata = new ArrayList<>();
    private List<Datapiechart> listdatapie = new ArrayList();
    //饼状图Item颜色
    private List<Integer> colors = new ArrayList<>();
    //柱状图数据
    private List<String> barlines = new ArrayList<>();
    private List<BarEntry> barvalues = new ArrayList<>();
    private List<BarEntry> barvalues2 = new ArrayList<>();
    private List<BarEntry> barvalues3 = new ArrayList<>();
    //柱形宽度
    private float width = 0.15f;
    private  DefPieChartSetting setting;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initVM();
        initDefLineChartData();
        initDefLineChartView();
        initDefPieChartData();
        initDefPieChartView();
        initDefBarChartData();
        initDefBarChartView();
    }

    protected void initVM() {
        mDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mDataBinding.setViewModel(this);
    }

    protected void initDefLineChartData(){
        lines.clear();
        lines.add("测试1");
        lines.add("测试2");

        //  x值 = 月份 - 1
        values.clear();
        values.add(new Entry(0, 1));
        values.add(new Entry(1, 3));
        values.add(new Entry(2, 2));
        values.add(new Entry(3, 4));
        values.add(new Entry(4, 3));
        values.add(new Entry(5, 2.3f));

        values2.clear();
        values2.add(new Entry(0, 2));
        values2.add(new Entry(1, 1));
        values2.add(new Entry(2, 4));
        values2.add(new Entry(3, 1));
        values2.add(new Entry(4, 3));
        values2.add(new Entry(5, 4));
    }

    protected void initDefLineChartView(){

        DefLineChartSetting setting = new DefLineChartSetting(this,mDataBinding.deflinechart);

        DefLineChartSetting.SetAttribute setDataAttribute = new DefLineChartSetting.SetAttribute();
        setDataAttribute.setting(setting)
                .label(lines)
                .mode(LineDataSet.Mode.CUBIC_BEZIER)
                .cubicintensity(0.2f)
                .color(Color.parseColor("#3299ff"))
                .circlecolor(Color.parseColor("#3299ff"))
                .linewidth(1f)
                .circleradius(3f)
                .highlightenabled(true)
                .highlightline(0f,5f,0f)
                .valuetextsize(0)
                .drawfilled(true)
                .filldrawable(ContextCompat.getDrawable(this, R.drawable.bg_gradient_ramp))
                .setData(values,0);

        DefLineChartSetting.SetAttribute setDataAttribute2 = new DefLineChartSetting.SetAttribute();
        setDataAttribute2.setting(setting)
                .label(lines)
                .mode(LineDataSet.Mode.CUBIC_BEZIER)
                .cubicintensity(0.2f)
                .color(Color.parseColor("#6db81f"))
                .circlecolor(Color.parseColor("#6db81f"))
                .linewidth(1f)
                .circleradius(3f)
                .highlightenabled(true)
                .highlightline(10f,5f,0f)
                .highlightlinewidth(1)
                .highlightcolor(Color.parseColor("#6db81f"))
                .valuetextsize(0)
                .drawfilled(true)
                .filldrawable(ContextCompat.getDrawable(this, R.drawable.bg_gradient_ramp))
                .mv(new PointDataView(setting.getContext(),R.drawable.bg_chart_num,9,Color.parseColor("#3299ff")))
                .setData(values2,1);

        DefLineChartSetting.SetChart setChartAttribute = new DefLineChartSetting.SetChart();
        setChartAttribute.setting(setting)
                .descriptionenabled(false)
                .touchenabled(true)
                .scaleenabled(false)
                .scalexenabled(false)
                .scaleyenabled(false)
                .doubletaptozoomenabled(false)
                .extrabottomoffset(5)
                .setChart();

        DefLineChartSetting.SetLegend setLegendAttribute = new DefLineChartSetting.SetLegend();
        setLegendAttribute.setting(setting)
                .enabled(true)
                .setLegend();

        DefLineChartSetting.SetXAxis setXAxisAttribute = new DefLineChartSetting.SetXAxis();
        setXAxisAttribute.setting(setting)
                .labelrotationangle(-15f)
                .drawgridlines(false)
                .axislinecolor(Color.parseColor("#e5ecfe"))
                .axislinewidth(1)
                .position(XAxis.XAxisPosition.BOTTOM)
                .axismaximum(5 + 0.3f)
                .axisminimum(-0.3f)
                .labelcount(5,false)
                .textcolor(Color.parseColor("#666680"))
                .setXAxis();
        setting.getxAxis().setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String tap = String.valueOf(value+1);
                return "2022-" + tap.substring(0, tap.indexOf("."));
            }
        });

        DefLineChartSetting.SetYAxis setYAxisAttribute = new DefLineChartSetting.SetYAxis();
        setYAxisAttribute.setting(setting)
                .drawgridlines(false)
                .axislinecolor(Color.parseColor("#e5ecfe"))
                .axislinewidth(1)
                .axismaximum(5f)
                .axisminimum(0f)
                .textcolor(Color.parseColor("#666680"))
                .labelcount(5,false)
                .axisrightenabled(false)
                .setYAxis();
        setting.getyAxis().setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if(value == 5f){
                    return "(星)";
                }
                return String.valueOf(value).substring(0,1);
            }
        });
    }

    protected void initDefPieChartData(){

        listdatapie.clear();
        listdatapie.add(new Datapiechart(Color.parseColor("#108ee9"),"居民小区",10,"人",10));
        listdatapie.add(new Datapiechart(Color.parseColor("#ef0000"),"娱乐场所",10,"人",10));
        listdatapie.add(new Datapiechart(Color.parseColor("#6db81f"),"交通道路",20,"人",20));
        listdatapie.add(new Datapiechart(Color.parseColor("#FF6200EE"),"城郊结合部",30,"人",30));
        listdatapie.add(new Datapiechart(Color.parseColor("#999999"),"其他",30,"人",30));

        listdata.clear();
        listdata.add(new PieEntry(listdatapie.get(0).getNum(),listdatapie.get(0).getLable()));
        listdata.add(new PieEntry(listdatapie.get(1).getNum(),listdatapie.get(1).getLable()));
        listdata.add(new PieEntry(listdatapie.get(2).getNum(),listdatapie.get(2).getLable()));
        listdata.add(new PieEntry(listdatapie.get(3).getNum(),listdatapie.get(3).getLable()));
        listdata.add(new PieEntry(listdatapie.get(4).getNum(),listdatapie.get(4).getLable()));

        colors.clear();
        colors.add(listdatapie.get(0).getLablecolor());
        colors.add(listdatapie.get(1).getLablecolor());
        colors.add(listdatapie.get(2).getLablecolor());
        colors.add(listdatapie.get(3).getLablecolor());
        colors.add(listdatapie.get(4).getLablecolor());
    }

    protected void initDefPieChartView(){
        DefPieChartSetting setting = new DefPieChartSetting(this,mDataBinding.defpiechart);

        DefPieChartSetting.SetAttribute setAttribute = new DefPieChartSetting.SetAttribute();
        setAttribute.setting(setting)
                .drawvalues(false)
                .descriptionenabled(false)
                .descriptiontext("测试")
                .backgroundcolor(Color.WHITE)
                .extraoffsets(0,0,0,0)
                .drawentrylabels(false)
                .rotationenabled(false)
                .usepercentvalues(true)
                .highlightpertapenabled(true)
                .drawholeenabled(true)
                .holeradius(75f)
                .holecolor(Color.WHITE)
                .drawcentertext(true)
                .centertext(String.valueOf(listdatapie.get(0).getNum()+listdatapie.get(1).getNum()+listdatapie.get(2).getNum()+listdatapie.get(3).getNum()+listdatapie.get(4).getNum()),"人口总数")
                .centertextcolor(Color.parseColor("#3299ff"),Color.BLACK)
                .centertextsize(25f,20f)
                .centertexttypeface(Typeface.DEFAULT_BOLD,Typeface.DEFAULT)
                .enabled(false)
                .setData(listdata,colors);

        //自定义图例
        //添加数据
        setting.getnAdapter().setItems(listdatapie);
        mDataBinding.defpiechartLegend.setLayoutManager(new LinearLayoutManager(setting.getContext(),
                RecyclerView.VERTICAL, false));
        mDataBinding.defpiechartLegend.setAdapter(setting.getnAdapter());

    }

    protected void initDefBarChartData(){
        barlines.clear();
        barlines.add("测试1");
        barlines.add("测试2");
        barlines.add("测试3");

        //  x值 = 月份 - 1
        barvalues.clear();
        barvalues.add(new BarEntry(0-width, 2));
        barvalues.add(new BarEntry(1-width, 1));
        barvalues.add(new BarEntry(2-width, 4));
        barvalues.add(new BarEntry(3-width, 1));
        barvalues.add(new BarEntry(4-width, 3));
        barvalues.add(new BarEntry(5-width, 4));

        barvalues2.clear();
        barvalues2.add(new BarEntry(0, 1));
        barvalues2.add(new BarEntry(1, 3));
        barvalues2.add(new BarEntry(2, 2));
        barvalues2.add(new BarEntry(3, 4));
        barvalues2.add(new BarEntry(4, 3));
        barvalues2.add(new BarEntry(5, 2.3f));

        barvalues3.clear();
        barvalues3.add(new BarEntry(0+width, 4));
        barvalues3.add(new BarEntry(1+width, 2));
        barvalues3.add(new BarEntry(2+width, 3));
        barvalues3.add(new BarEntry(3+width, 4));
        barvalues3.add(new BarEntry(4+width, 1));
        barvalues3.add(new BarEntry(5+width, 3));
    }

    protected void initDefBarChartView(){

        DefBarChartSetting setting = new DefBarChartSetting(this,mDataBinding.defbarchart);

        DefBarChartSetting.SetAttribute setDataAttribute = new DefBarChartSetting.SetAttribute();
        setDataAttribute.setting(setting)
                .label(barlines)
                .drawvalues(false)
                .textColor(Color.parseColor("#3299ff"))
                .textsize(10f)
                .color(Color.parseColor("#3299ff"))
                .fill(new Fill(Color.parseColor("#ffffff"),Color.parseColor("#3299ff")))
                .barwidth(width)
                .setData(barvalues,0);

        DefBarChartSetting.SetAttribute setDataAttribute2 = new DefBarChartSetting.SetAttribute();
        setDataAttribute2.setting(setting)
                .label(barlines)
                .drawvalues(false)
                .textColor(Color.parseColor("#6db81f"))
                .textsize(10f)
                .color(Color.parseColor("#6db81f"))
                .fill(new Fill(Color.parseColor("#ffffff"),Color.parseColor("#6db81f")))
                .barwidth(width)
                .setData(barvalues2,1);


        DefBarChartSetting.SetAttribute setDataAttribute3 = new DefBarChartSetting.SetAttribute();
        setDataAttribute3.setting(setting)
                .label(barlines)
                .drawvalues(false)
                .textColor(Color.parseColor("#FF0000"))
                .textsize(10f)
                .color(Color.parseColor("#FF0000"))
                .fill(new Fill(Color.parseColor("#ffffff"),Color.parseColor("#FF0000")))
                .barwidth(width)
                .mv(new PointDataView(setting.getContext(),R.drawable.bg_chart_num,9,Color.parseColor("#6db81f")))
                .setData(barvalues3,2);

        DefBarChartSetting.SetChart setChartAttribute = new DefBarChartSetting.SetChart();
        setChartAttribute.setting(setting)
                .descriptionenabled(false)
                .touchenabled(true)
                .scaleenabled(false)
                .scalexenabled(false)
                .scaleyenabled(false)
                .doubletaptozoomenabled(false)
                .extrabottomoffset(5)
                .setChart();

        DefBarChartSetting.SetLegend setLegendAttribute = new DefBarChartSetting.SetLegend();
        setLegendAttribute.setting(setting)
                .enabled(true)
                .setLegend();

        DefBarChartSetting.SetXAxis setXAxisAttribute = new DefBarChartSetting.SetXAxis();
        setXAxisAttribute.setting(setting)
                .labelrotationangle(-15f)
                .drawgridlines(false)
                .axislinecolor(Color.parseColor("#e5ecfe"))
                .axislinewidth(1)
                .position(XAxis.XAxisPosition.BOTTOM)
                .axismaximum(5 + 0.5f)
                .axisminimum(-0.5f)
                .labelcount(5,false)
                .textcolor(Color.parseColor("#666680"))
                .setXAxis();
        setting.getxAxis().setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String tap = String.valueOf(value+1);
                return "2022-" + tap.substring(0, tap.indexOf("."));
            }
        });

        DefBarChartSetting.SetYAxis setYAxisAttribute = new DefBarChartSetting.SetYAxis();
        setYAxisAttribute.setting(setting)
                .drawgridlines(false)
                .axislinecolor(Color.parseColor("#e5ecfe"))
                .axislinewidth(1)
                .axismaximum(5f)
                .axisminimum(0f)
                .textcolor(Color.parseColor("#666680"))
                .labelcount(5,false)
                .axisrightenabled(false)
                .setYAxis();
        setting.getyAxis().setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if(value == 5f){
                    return "(星)";
                }
                return String.valueOf(value).substring(0,1);
            }
        });
    }

}